home *** CD-ROM | disk | FTP | other *** search
Wrap
Public Class ValidationForm Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents RangeValidator1 As System.Web.UI.WebControls.RangeValidator Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator Protected WithEvents Label5 As System.Web.UI.WebControls.Label Protected WithEvents CustomValidator1 As System.Web.UI.WebControls.CustomValidator Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox Protected WithEvents txtYearBorn As System.Web.UI.WebControls.TextBox Protected WithEvents txtYearMarried As System.Web.UI.WebControls.TextBox Protected WithEvents txtPhoneNumber As System.Web.UI.WebControls.TextBox Protected WithEvents Label7 As System.Web.UI.WebControls.Label Protected WithEvents CompareValidator2 As System.Web.UI.WebControls.CompareValidator Protected WithEvents txtLastVisit As System.Web.UI.WebControls.TextBox Protected WithEvents Label8 As System.Web.UI.WebControls.Label Protected WithEvents txtChildren As System.Web.UI.WebControls.TextBox Protected WithEvents CompareValidator3 As System.Web.UI.WebControls.CompareValidator Protected WithEvents txtEvenNumber As System.Web.UI.WebControls.TextBox Protected WithEvents lblStatus As System.Web.UI.WebControls.Label Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here ' this constant means that the JavaScript of the custom ' function is stored right in the HTML page ' (If you set this constant to False you must delete the ' <script> portion of the HTML page) #Const EMBED_CUSTOM_FUNCTION_IN_HTML = True #If EMBED_CUSTOM_FUNCTION_IN_HTML = False Then ' this block of code shows how you can embed the JavaScript ' routine via code, by assigning it to a Literal control Literal1.Text = "<script language=""JScript""><!-- " & ControlChars.CrLf _ & "function CheckEvenNumber(sender, args) {" & ControlChars.CrLf _ & " var theNumber = args.Value;" & ControlChars.CrLf _ & " if (theNumber % 2 == 0) " & ControlChars.CrLf _ & " args.IsValid = true;" & ControlChars.CrLf _ & " else " & ControlChars.CrLf _ & " args.IsValid = false;" & ControlChars.CrLf _ & "} " & ControlChars.CrLf _ & "//--> " & ControlChars.CrLf _ & "</script>" & ControlChars.CrLf #End If ' this code shows how you can remember the intial non-null ' value of the txtUserName control. If txtUserName.Attributes("InitialValue") = "" Then If txtUserName.Text.Length > 0 Then txtUserName.Attributes.Add("initialvalue", txtUserName.Text) End If End If End Sub ' this is the server-side version of the custom validate function Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate Dim theNumber As Integer = CInt(args.Value) If (theNumber Mod 2) = 0 Then args.IsValid = True Else args.IsValid = False End If End Sub ' this routine runs when the form is submitted Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Force the validation of all controls. Me.Validate() ' the form might be nonvalid only if working on a browser ' that doesn't support JavaScript, or if we've set the ' EnableClientScript property to False If Not Me.IsValid Then Dim ctrl As BaseValidator Dim errorCount As Integer ' Count how many errors were found. For Each ctrl In Me.Validators If Not ctrl.IsValid Then errorCount += 1 Next ' Display a suitable error message. lblStatus.Text = "There are " & errorCount.ToString & " errors." End If End Sub End Class